const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userAvatar: "https://studynest.top/img/userImage.jpg",
    email: "853137327@qq.com",
    userName: "引号",
    userProfile: ''
  },
  //检测姓名输入是否为空
  inputName(name) {
    if (!name) {
      getApp().showMessage('请输入姓名')
      return false
    } else {
      // console.log(name);
      return true
    }
  },
  //检测邮箱输入是否正确
  inputEmail(e) {
    let str =/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    if(!str.test(e)){
      getApp().showMessage('请填写正确的邮箱号')
    }
    return str.test(e)
  },
  //保存编辑
  async save(e) {
    const newUserInfo = e.detail.value;
    // console.log(this.inputName(newUserInfo.userName))
    // console.log(this.inputEmail(newUserInfo.email))
    const isOk = this.inputName(newUserInfo.userName) && this.inputEmail(newUserInfo.email);
    if (!isOk) return;
    this.setData({
      userName: newUserInfo.userName,
      email: newUserInfo.email
    })
    const that = this;
    wx.request({
      url: app.globalData.domain + '/api/user/update/my',
      method: "post",
      header: {
        "token": wx.getStorageSync('token')
      },
      data: that.data,
      success: function (res) {
        wx.showToast({
          title: "修改成功!",
          icon: 'success'
        })
        //成功后自动退回到上一页
        setTimeout(()=>{
          wx.navigateBack()
        },1000)
      },
      fail: function (err) {
        wx.showToast({
          title: "修改失败!",
          icon: 'error'
        })
        console.error(err)
      }
    })
  },
  //选择头像
  async chooseAvatar(e) {
    const that = this;
    // console.log(e.detail)
    const avatarUrl = e.detail.avatarUrl
    //发送请求到服务器保存图片，并且记录返回链接
    const newAvatar = await that.uploadAvatar(avatarUrl)
    //渲染图片至页面
    that.setData({
      userAvatar: newAvatar
    })
  },
  //上传用户头像
  uploadAvatar(filePath) {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        filePath: filePath,
        name: 'image',
        url: app.globalData.domain + '/api/file/uploadImage',
        header: {
          "token": wx.getStorageSync('token')
        },
        success: function (res) {
          const resData = JSON.parse(res.data)
          // console.log(resData)
          if (resData.code === 200) {
            resolve(resData.data)
          } else {
            reject("上传头像失败!")
          }

        },
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('用户信息',app.globalData.userInfo)
    this.setData({
      userName: app.globalData.userInfo.userName,
      userAvatar: app.globalData.userInfo.userAvatar,
      email: app.globalData.userInfo.email,
      userProfile: app.globalData.userInfo.userProfile
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})